@file:Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")

package com.sunshine.lnuplus.view

import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import com.sunshine.lnuplus.R
import java.lang.Exception
import java.util.*

/**
 * Created by sunshine on 2020/3/15
 */

object ShortCut {

    fun init(context: Context) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1){
            val addedFunctionList = ArrayList<ArrayList<String>>()
            val appSetting = context.getSharedPreferences("com.sunshine.lnuplus_preferences",Context.MODE_PRIVATE)
            val intent = Intent()
            appSetting.getString("short_cut", "成绩查询#6")?.split("*")?.forEach {
                try {
                    val arrayList = arrayListOf(it.split("#")[0], it.split("#")[1])
                    addedFunctionList.add(arrayList)
                }catch (e: Exception){}
            }
            context.getSystemService(ShortcutManager::class.java).removeAllDynamicShortcuts()
            addedFunctionList.forEach {
                when (it[1]) {
                    "1" -> {
                        intent.action = "com.sunshine.lnuplus.AppSetting"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.AppSettingActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "AppSetting")
                                .setIntent(intent)
                                .setShortLabel("软件设置")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_settings))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "2" -> {
                        intent.action = "com.sunshine.lnuplus.TimetableSetting"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.TimetableSettingActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "TimetableSetting")
                                .setIntent(intent)
                                .setShortLabel("课表设置")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_settings))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "3" -> {
                        intent.action = "com.sunshine.lnuplus.WebView1"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.WebViewActivity")
                        val bundle = Bundle()
                        bundle.putString("title","常见问题")
                        bundle.putString("url","https://support.qq.com/embed/phone/131050/faqs-more?")
                        intent.putExtras(bundle)
                        val builder =
                            ShortcutInfo.Builder(context, "WebView1")
                                .setIntent(intent)
                                .setShortLabel("常见问题")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_help))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "4" -> {
                        intent.action = "com.sunshine.lnuplus.WebView2"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.WebViewActivity")
                        val bundle = Bundle()
                        bundle.putString("title","反馈")
                        bundle.putString("url","https://support.qq.com/product/131050")
                        intent.putExtras(bundle)
                        val builder =
                            ShortcutInfo.Builder(context, "WebView2")
                                .setIntent(intent)
                                .setShortLabel("反馈")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_question))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "5" -> {
                        intent.action = "com.sunshine.lnuplus.About"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.AboutActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "About")
                                .setIntent(intent)
                                .setShortLabel("关于")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_about))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "6" -> {
                        intent.action = "com.sunshine.lnuplus.SearchScore"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.SearchScoreActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "SearchScore")
                                .setIntent(intent)
                                .setShortLabel("成绩查询")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_search))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "7" -> {
                        intent.action = "com.sunshine.lnuplus.SchoolDate"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.SchoolDateActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "SchoolDate")
                                .setIntent(intent)
                                .setShortLabel("校历")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_calender))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    "8" -> {
                        intent.action = "com.sunshine.lnuplus.Calender"
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.CalenderActivity")
                        val builder =
                            ShortcutInfo.Builder(context, "Calender")
                                .setIntent(intent)
                                .setShortLabel("导出到日历")
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_calender))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                    else -> {
                        intent.action = "com.sunshine.lnuplus" + it[1]
                        intent.setClassName(
                            "com.sunshine.lnuplus",
                            "com.sunshine.lnuplus.ui.activity.WebViewActivity")
                        val bundle = Bundle()
                        bundle.putString("title",it[0])
                        bundle.putString("url",it[1])
                        intent.putExtras(bundle)
                        val builder =
                            ShortcutInfo.Builder(context, it[1])
                                .setIntent(intent)
                                .setShortLabel(it[0])
                                .setIcon(Icon.createWithResource(context, R.drawable.ic_function))
                        context.getSystemService(ShortcutManager::class.java).addDynamicShortcuts(listOf(builder.build()))
                    }
                }
            }

        }

    }
}